﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CS633_Final_Project
{
    /// <summary>
    /// A line segment.
    /// </summary>
    class Line
    {
        private PointF p;
        private PointF q;
        private double length;

        /// <summary>
        /// Constructs a line segment from the given points.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public Line(PointF p, PointF q)
        {
            this.p = p;
            this.q = q;
            CalcLength();
        }

        /// <summary>
        /// String representation of a line.
        /// </summary>
        /// <returns>String representation of a line.</returns>
        public override string ToString()
        {
            return "Line Segment: (" + p.ToString() + "),(" + q.ToString() + ")";
        }

        /// <summary>
        /// Returns whether or not the two Lines are equal
        /// </summary>
        /// <param name="obj">Another Line</param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            Line anotherLine = (Line)obj;
            return (this.P.Equals(anotherLine.P)
                && this.Q.Equals(anotherLine.Q))
                || (this.P.Equals(anotherLine.Q)
                && this.Q.Equals(anotherLine.P));

        }

        /// <summary>
        /// Generates a hashcode for the line.
        /// </summary>
        /// <returns>Hashcode for the line</returns>
        public override int GetHashCode()
        {
            return this.p.GetHashCode() ^ this.q.GetHashCode();
        }

        /// <summary>
        /// X endpoint.
        /// </summary>
        public PointF P
        {
            get { return this.p; }
            set { this.p = value; }
        }

        /// <summary>
        /// Y endpoint.
        /// </summary>
        public PointF Q
        {
            get { return this.q; }
            set { this.q = value; }
        }

        /// <summary>
        /// Returns the length of the line segment.
        /// </summary>
        public double Length
        {
            get { return this.length; }
            set { this.length = value; }
        }

        private void CalcLength()
        {
            double x1 = (double)p.X;
            double x2 = (double)q.X;
            double y1 = (double)p.Y;
            double y2 = (double)q.Y;

            length = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); ;
        }
    }
}
